package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Employee;
import com.example.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee) {

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee employee1 = employeeService.getOne(wrapper);

        if (employee1 == null) {
            return R.error("用户名不存在！");
        }

        if (!password.equals(employee1.getPassword())) {
            return R.error("密码错误！");
        }


        if(employee1.getStatus()==0){
            return R.error("该账户已被禁用！");
        }

        request.getSession().setAttribute("employee", employee1.getId());


        return R.success(employee1);


    }


    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }

    @CacheEvict(value = "employees", allEntries = true)
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee) {

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));



        employeeService.save(employee);

        return R.success("新员工添加成功！");


    }


    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name) {

        Page p = (Page) redisTemplate.opsForValue().get("employees");

        if(p!=null){
            return R.success(p);
        }
        Page pg = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();

        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name).notLike(Employee::getUsername,"admin");

        wrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pg, wrapper);

        redisTemplate.opsForValue().set("employees", pg,3600L, TimeUnit.SECONDS);

        return R.success(pg);
    }


    @CacheEvict(value = "employees", allEntries = true)
    @PutMapping
    public R<Employee> update(HttpServletRequest request,@RequestBody Employee employee){

        Long employee1 =(Long) request.getSession().getAttribute("employee");

        employeeService.updateById(employee);


        return R.success(employee);
    }


    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        if(employee!=null) return R.success(employee);

        return R.error("未查询到员工信息");

    }

}
